<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hadd('')"
                      >添加子部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          default-expand-all
          :props="{ children: 'children', label: 'name' }"
        >
          <!-- 作用域插槽 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hadd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdit(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹出对话框 -->
      <el-dialog
        :title="isEdit ? '编辑部门' : '添加部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <employees
          v-if="showDialog"
          :id="curId"
          @success="hSuccess"
          :isEdit="isEdit"
          @close="showDialog = false"
          :originList="originList"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import employees from "./deptDialog.vue";
import { getDepartments, delDepartments } from "@/api/departments";
import { tranDataToTree } from "@/utils/index";
export default {
  components: {
    employees,
  },
  data() {
    return {
      list: [], // 定义存储树状列表
      showDialog: false,
      curId: "",
      isEdit: false, // false 添加 || true 编辑
      originList: "",
    };
  },
  created() {
    // 初始化数据
    this.loadDepartments();
  },
  methods: {
    // 添加
    hadd(id) {
      this.curId = id;
      this.isEdit = false;
      this.showDialog = true;
    },
    // 编辑
    hEdit(id) {
      this.curId = id;
      this.isEdit = true;
      this.showDialog = true;
    },
    // 删除
    hDel(id) {
      this.$confirm("您确定删除此部门吗??")
        .then(async () => {
          // 删除请求
          const res = await delDepartments(id);
          // console.log(res);
          if (res.code !== 10000) return this.$message.error(res.message);
          this.$message.success(res.message);
          this.loadDepartments();
        })
        .catch((error) => console.log(error));
    },
    // 接收子组件触发的函数 关闭对话框
    hSuccess() {
      this.loadDepartments();
      this.showDialog = false;
    },
    // 渲染树状列表
    async loadDepartments() {
      try {
        // 发送请求
        const res = await getDepartments();
        // console.log("getDepartments", res.data.depts);
        // 删除第一个
        res.data.depts.shift();
        // 调用函数的值并传入
        this.list = tranDataToTree(res.data.depts);
        this.originList = res.data.depts.map((item) => {
          return {
            code: item.code,
            id: item.id,
            name: item.name,
            pid: item.pid,
          };
        });
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>